#pragma once
#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctime>
#include <cmath>

time_t parseDateTime(const std::string &datetime)
{
    std::tm tm = {};
    std::istringstream ss(datetime);
    ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
    return mktime(&tm); // 返回 time_t 类型，单位为秒
}

int computeTimeDifference(const std::string &exit_time, const std::string &current_time)
{
    time_t t1 = parseDateTime(exit_time);
    time_t t2 = parseDateTime(current_time);
    return static_cast<int>(difftime(t2, t1)); // 返回时间差（秒）
}

double calculateParkingFee(int duration_seconds)
{
    const int base_seconds = 30 * 60;
    const double base_fee = 3.0;
    const double extra_fee_per_half_hour = 2.0;

    if (duration_seconds <= base_seconds)
    {
        return base_fee;
    }
    else
    {
        int extra_seconds = duration_seconds - base_seconds;
        int extra_half_hours = std::ceil(extra_seconds / 1800.0); // 不足半小时按半小时计费
        return base_fee + extra_half_hours * extra_fee_per_half_hour;
    }
}

bool isValidDate(std::string stat_date, std::string date) // stat_date <= date  "xxxx-xx-xx"
{
    
    int yearStr1 = stoi(stat_date.substr(0, 4));
    int monthStr1 = stoi(stat_date.substr(5, 2));
    int dayStr1 = stoi(stat_date.substr(8, 2));

    int yearStr2 = stoi(date.substr(0, 4));
    int monthStr2 = stoi(date.substr(5, 2));
    int dayStr2 = stoi(date.substr(8, 2));
    std::cout << yearStr1 << "  " << monthStr1 <<" "<< dayStr1<<std::endl;
     std::cout << yearStr2 << "  " << monthStr2 <<" "<< dayStr2<<std::endl;

    if (yearStr1 < yearStr2)
    {
        return true;
    }
    else if (yearStr1 > yearStr2)
    {
        return false;
    }
    else
    {
        if (monthStr1 < monthStr2)
        {
            return true;
        }
        else if (monthStr1 > monthStr2)
        {
            return false;
        }
        else
        {
            return (dayStr1 <= dayStr2);
        }
    }
}